You can use the functions FSRead , FSWrite , and FSClose to read data from a file, write data to a file, and close an open file. All three of these functions operate on open files. You can use any one of a variety of routines to open a file (for example, FSpOpenDF ).
You can use the FSRead function to read any number of bytes from an open file.
FUNCTION FSRead (refNum: Integer; VAR count: LongInt;
buffPtr: Ptr): OSErr;
The FSRead function attempts to read the requested number of bytes from the specified file into the specified buffer. The buffPtr parameter points to that buffer; this buffer is allocated by your application and must be at least as large as the count parameter.
Because the read operation begins at the current mark, you might want to set the mark first by calling the SetFPos function. If you try to read past the logical end-of-file, FSRead reads in all the data up to the end-of-file, moves the mark to the end-of-file, and returns eofErr as its function result. Otherwise, FSRead moves the file mark to the byte following the last byte read and returns noErr .
You can use the FSWrite function to write any number of bytes to an open file.
FUNCTION FSWrite (refNum: Integer; VAR count: LongInt;
buffPtr: Ptr): OSErr;
The FSWrite function takes the specified number of bytes from the specified data buffer and attempts to write them to the specified file. Because the write operation begins at the current mark, you might want to set the mark first by calling the SetFPos function.
If the write operation completes successfully, FSWrite moves the file mark to the byte following the last byte written and returns noErr . If you try to write past the logical end-of-file, FSWrite moves the logical end-of-file. If you try to write past the physical end-of-file, FSWrite adds one or more clumps to the file and moves the physical end-of-file accordingly.
You can use the FSClose function to close an open file.
FUNCTION FSClose (refNum: Integer): OSErr;
The FSClose function removes the access path for the specified file and writes the contents of the volume buffer to the volume.
The FSClose function calls PBFlushFile internally to write the file's bytes onto the volume. To ensure that the file's catalog entry is updated, you should call FlushVol after you call FSClose .<36pt>
Make sure that you do not call FSClose with a file reference number of a file that has already been closed. Attempting to close the same file twice may result in loss of data on a volume. See the description of file control blocks in the chapter "File Manager" in this book for a discussion of how this can happen.<36pt>